package gui;

import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;

import com.bbn.openmap.MouseDelegator;
import com.bbn.openmap.event.MapMouseMode;
import com.bbn.openmap.event.PanMouseMode;
import com.bbn.openmap.event.SelectMouseMode;
import com.bbn.openmap.gui.menu.MouseModeMenu;
import com.bbn.openmap.tools.drawing.OMDrawingToolMouseMode;

import languages.Language;

public class Menu extends JMenuBar implements ActionListener{
	
	private JMenu menu;
	private JMenu submenu;
	private JMenuItem menuItem;
	private JRadioButtonMenuItem radioMenu[];
	
	private Language language;
	private Window window;
	
	private MouseDelegator mouseDelegator;
	private MapMouseMode modes[];
//	private PanMouseMode mouseDrag;
//	private SelectMouseMode mouseSelect;
//	private OMDrawingToolMouseMode mouseDraw;
	
	private DialogInsertPoint diPoint;
	private DialogInsertArc diArc;
	
	public Menu(Language lang, Window window){
		super();
		
		language = lang;
		this.window = window;
		
		this.add(fileMenu());
		this.add(insertMenu());
		this.add(mouseMenu());
		this.add(layerMenu());
		
//		mouseDrag = new PanMouseMode();
//		mouseSelect = new SelectMouseMode();
//		mouseDraw = new OMDrawingToolMouseMode();
		
//		this.window.getMapHandler().add(mouseDrag);
		
		mouseDelegator = this.window.getMouseDelegator();
		modes = mouseDelegator.getMouseModes();
//		if(mouseDelegator != null){
//			System.out.println("DZIALA :D");
//		}
		
		this.add(Box.createHorizontalGlue());
		
		this.add(aboutMenu());
		
	}
	

	private JMenu layerMenu() {
		menu = null;
		
		menu = new JMenu("Warstwy");
		
		menuItem = new JCheckBoxMenuItem("Pokaz wezly");
		menu.add(menuItem);
		
		menuItem = new JCheckBoxMenuItem("Pokaz krawedzie");
		menu.add(menuItem);
		
		return menu;
		
	}


	private JMenu mouseMenu() {
		menu = null;
		
		radioMenu = new JRadioButtonMenuItem[4];
		
		ButtonGroup group = new ButtonGroup();
		
		menu = new JMenu(language.menuMouseAction());
		menu.setMnemonic(KeyEvent.VK_M);
		
		radioMenu[0] = new JRadioButtonMenuItem(language.menuMouseActionDrag());
		radioMenu[0].setMnemonic(KeyEvent.VK_P);
		radioMenu[0].addActionListener(this);
		radioMenu[0].setActionCommand("M_DRAG"); //M_DRAG
		radioMenu[0].setSelected(true);
		group.add(radioMenu[0]);
		menu.add(radioMenu[0]);
		
		radioMenu[1] = new JRadioButtonMenuItem(language.menuMouseActionSelect());
		radioMenu[1].setMnemonic(KeyEvent.VK_Z);
		radioMenu[1].addActionListener(this);
		radioMenu[1].setActionCommand("M_SELECT"); //M_SELECT
		group.add(radioMenu[1]);
		menu.add(radioMenu[1]);
		
		radioMenu[2] = new JRadioButtonMenuItem(language.menuMouseActionAddPoint());
		radioMenu[2].setMnemonic(KeyEvent.VK_U);
		radioMenu[2].addActionListener(this);
		radioMenu[2].setActionCommand("M_POINT"); //M_POINT
		group.add(radioMenu[2]);
		menu.add(radioMenu[2]);
		
		radioMenu[3] = new JRadioButtonMenuItem(language.menuMouseActionAddArc());
		radioMenu[3].setMnemonic(KeyEvent.VK_D);
		radioMenu[3].addActionListener(this);
		radioMenu[3].setActionCommand("M_ARC"); //M_ARC
		group.add(radioMenu[3]);
		menu.add(radioMenu[3]);
		
		return menu;
	}

	private JMenu insertMenu() {
		menu = null;
		
		menu = new JMenu(language.menuInsert());
		menu.setMnemonic(KeyEvent.VK_W);
		
		menuItem = new JMenuItem(language.menuInsertPoint());
		menuItem.setMnemonic(KeyEvent.VK_P);
		menuItem.addActionListener(this);
		menuItem.setActionCommand("I_POINT"); //I_POINT
		menu.add(menuItem);
		
		menuItem = new JMenuItem(language.menuInsertArc());
		menuItem.setMnemonic(KeyEvent.VK_K);
		menuItem.addActionListener(this);
		menuItem.setActionCommand("I_ARC"); //I_ARC
		menu.add(menuItem);
		
		return menu;
	}

	private JMenu fileMenu() {
		menu = null;
		
		menu = new JMenu(language.menuFile());
		menu.setMnemonic(KeyEvent.VK_P);
		
		menuItem = new JMenuItem(language.menuFileOpen());
		menuItem.setMnemonic(KeyEvent.VK_W);
		menuItem.addActionListener(this);
		menuItem.setActionCommand("OPEN"); //OPEN
		menu.add(menuItem);
		
		menu.addSeparator();
		
		menuItem = new JMenuItem(language.menuFileClose());
		menuItem.setMnemonic(KeyEvent.VK_Z);
		menuItem.addActionListener(this);
		menuItem.setActionCommand("CLOSE"); //CLOSE
		menu.add(menuItem);
		
		return menu;
	}
	
	private JMenu aboutMenu() {
		menu = null;
		
		menu = new JMenu(language.menuAbout());
		menu.setMnemonic(KeyEvent.VK_O);
		
		menuItem = new JMenuItem(language.menuAboutOM());
		menuItem.setMnemonic(KeyEvent.VK_M);
		menuItem.addActionListener(this);
		menuItem.setActionCommand("OM"); //OM
		menu.add(menuItem);
		
		menuItem = new JMenuItem(language.menuAboutGIS());
		menuItem.setMnemonic(KeyEvent.VK_G);
		menuItem.addActionListener(this);
		menuItem.setActionCommand("GIS"); //GIS
		menu.add(menuItem);
		
		menu.addSeparator();
		
		menuItem = new JMenuItem(language.menuAboutCreator());
		menuItem.setMnemonic(KeyEvent.VK_C);
		menuItem.addActionListener(this);
		menuItem.setActionCommand("I");	//I
		menu.add(menuItem);
		
		return menu;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
		String event = e.getActionCommand();
		
		System.out.println(event);
		
		if("OPEN".equals(event)){
			System.out.println("Open");
		}
		else if("CLOSE".equals(event)){
			System.exit(0);
		}
		else if("OM".equals(event)){
			Desktop d = Desktop.getDesktop();
			try {
				URI uri = new URI("http://openmap.bbn.com");
					d.browse(uri);
			} catch (URISyntaxException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		else if("GIS".equals(event)){
			Desktop d = Desktop.getDesktop();
			try {
				URI uri = new URI("http://www.gis.com/");
					d.browse(uri);
			} catch (URISyntaxException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		else if("I".equals(event)){
			new DialogAbout(window, language);
		}
		////MOUSE
		else if("M_DRAG".equals(event)){
			setMouseModeById(0);
		}
		else if("M_SELECT".equals(event)){
			setMouseModeById(1);
//			mouseDelegator.setActiveMouseMode(mouseSelect);
//			MapMouseMode[] modes = mouseDelegator.getMouseModes();
//			for(int i=0; i<modes.length; i++){
//				System.out.println(modes[i]);
//			}
//			mouseDelegator.setActiveMouseModeWithID();
		}
		else if("M_POINT".equals(event)){
//			mouseDelegator.setActiveMouseMode(mouseDraw);
		}
		else if("M_ARC".equals(event)){
//			mouseDelegator.setActiveMouseMode(mouseDraw);
		}
		else if("I_POINT".equals(event)){
			new DialogInsertPoint(window, language);
		}
		else if("I_ARC".equals(event)){
			new DialogInsertArc(window, language);
		}
		
		
	}
	
	public void setMouseModeById(int id){
		if(id<4){
			radioMenu[id].setSelected(true);
			mouseDelegator.setActiveMouseModeWithID(modes[id].getID());
		}
	}
	
}
